package com.zzw.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzw.dao.PersonDao;
import com.zzw.model.LoginInfo;
import com.zzw.model.Person;
import com.zzw.model.PersonQuery;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/yunji-api/person")
public class PersonController {
    @Resource
    private PersonDao personDao;

    @PostMapping("/insert")
    public Integer insert(@RequestBody Person person) {
        return personDao.insert(person);
    }

    @GetMapping("/select")
    public PageInfo<Person> select(PersonQuery query) {
        if (ObjectUtil.isNotEmpty(query.getPageNum()) && ObjectUtil.isNotEmpty(query.getPageSize())) {
            PageHelper.startPage(query.getPageNum(), query.getPageSize());
        }
        List<Person> persons = personDao.select(query);
        return new PageInfo<>(persons);
    }

    @DeleteMapping("/delete")
    public Integer delete(@RequestParam String id) {
        return personDao.delete(id);
    }

    @PutMapping("/update")
    public Integer update(@RequestBody Person person) {
        return personDao.update(person);
    }

    @PutMapping("/update/face-flag")
    public Integer updateFaceFlag(@RequestBody Person person) {
        return personDao.updateFaceFlag(person);
    }

    @PostMapping("/login")
    public Person login(@RequestBody LoginInfo loginInfo) {
        Person person = personDao.login(loginInfo);
        if (Objects.isNull(person)) {
            throw new RuntimeException("用户未找到！");
        }
        return person;
    }
}
